# 使用提醒:
# 1. xbot包提供软件自动化、数据表格、Excel、日志、AI等功能
# 2. package包提供访问当前应用数据的功能，如获取元素、访问全局变量、获取资源文件等功能
# 3. 当此模块作为流程独立运行时执行main函数
# 4. 可视化流程中可以通过"调用模块"的指令使用此模块

#import xbot
#from xbot import print, sleep
#from .import package
#from .package import variables as glv

def main(args):
    pass
import time
import struct
import base64
from gmssl import sm3
from functools import reduce
from Cryptodome.Cipher import ARC4  # 使用 Cryptodome 或 Cryptodome.Cipher

class BogusCreator:
    def __init__(self):
        self.rc4_key = bytes([0, 1, 14])
        self.env_data = b'1536|747|1536|834|0|30|0|0|1536|834|1536|864|1525|747|24|24|Win32'

    def create_bogus(self, url, user_agent, data=''):
        current_time_ms = int(time.time() * 1000)
        user_agent_hash = self.compute_sm3_hash(self.custom_base64_encode(
            ARC4.new(self.rc4_key).encrypt(user_agent.encode()), 'ckdp1h4ZKsUB80/Mfvw36XIgR25+WQAlEi7NLboqYTOPuzmFjJnryx9HVGDaStCe='))
        url_hash = self.compute_sm3_hash(self.compute_sm3_hash((url + 'cus').encode()))
        data_hash = self.compute_sm3_hash(self.compute_sm3_hash((data + 'cus').encode()))
        end_time_ms = int(time.time() * 1000)
        message_body = bytearray(32 * 3)
        message_body[18] = 44
        message_body[20: 24] = struct.pack(">I", current_time_ms & 0xFFFFFFFF)
        message_body[24: 26] = struct.pack("<H", current_time_ms >> 32)
        message_body[26: 30] = struct.pack(">I", self.rc4_key[0])
        message_body[30: 32] = bytes([(self.rc4_key[1] >> 8) & 0xFF, self.rc4_key[1] & 0xFF])
        message_body[32: 34] = struct.pack(">H", self.rc4_key[1] >> 16)
        message_body[34: 38] = struct.pack(">I", self.rc4_key[2])
        message_body[38: 40] = url_hash[21: 23]
        message_body[40: 42] = data_hash[21: 23]
        message_body[42: 44] = user_agent_hash[23: 25]
        message_body[44: 48] = struct.pack(">I", end_time_ms & 0xFFFFFFFF)
        message_body[48] = 3
        message_body[49: 51] = struct.pack("<H", end_time_ms >> 32)
        message_body[52: 56] = struct.pack(">I", 6241)
        message_body[57: 61] = struct.pack("<I", 6383)
        message_body[65: 67] = struct.pack("<H", len(self.env_data))
        message_body[70: 72] = struct.pack("<H", 0)

        final_bytes = bytes([
            message_body[i] for i in [
                18, 20, 52, 26, 30, 34, 58, 38, 40, 53, 42, 21,
                27, 54, 55, 31, 35, 57, 39, 41, 43, 22, 28, 32,
                60, 36, 23, 29, 33, 37, 44, 45, 59, 46, 47, 48,
                49, 50, 24, 25, 65, 66, 70, 71
            ]
        ])

        final_bytes += self.env_data + bytes([reduce(lambda x, y: x ^ y, final_bytes)])
        encoded_request = self.custom_base64_encode(
            b"\x03F\x07=#E\x02\x15\x8b\x11'\x04" + ARC4.new(b'y').encrypt(final_bytes),
            'Dkdpgh2ZmsQB80/MfvV36XI1R45-WUAlEixNLwoqYTOPuzKFjJnry79HbGcaStCe=').decode()
        return encoded_request

    def compute_sm3_hash(self, data):
        return bytes.fromhex(sm3.sm3_hash(list(data)))

    def custom_base64_encode(self, message, custom_base64_table):
        translation_table = str.maketrans(
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
            custom_base64_table
        )
        return base64.b64encode(message).decode().translate(translation_table).encode()

if __name__ == '__main__':
    # 如果需要直接运行这个脚本，可以在这里添加测试代码
    pass
